<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>测试页面-了解websocket</title>
  </head>
  <body>
    <button>点击发消息</button>
    <script>
      // 建立一个websocket实例
      let ws = new WebSocket('ws://124.222.224.186:8800/')
      ws.onopen = () => {
        alert('连接成功')
      }
      // 监听消息
      ws.onmessage = (e) => {
        console.log(e)
      }
      // 监听关闭
      ws.onclose = () => {
        alert('连接关闭')
      }
      // 发消息
      document.querySelector('button').onclick = () => {
        ws.send('hello')
      }
      // 收消息
      ws.onmessage = (e) => {
        console.log(e.data)
      }
    </script>
  </body>
</html>
